#define _CRT_SECURE_NO_WARNINGS 


#include <stdio.h>
#include <stdlib.h>
#include <assert.h>


typedef struct libos
{
	int v;
	struct libos* next;
	
}libo;

libo* CreNd(int a)
{
	libo* p = (libo*)malloc(sizeof(libo));
	if (p == NULL)
	{
		perror("malloc");
		exit(1);
	}
	p->next = NULL;
	
	p->v = 0;
	return p;
}

libo* He_Bin(libo* p1, libo* p2)
{
	if (p1 == NULL || p2 == NULL)
	{
		return p1 ? p1 : p2;
	}
	
	libo* phead = CreNd(0);
	libo* cur = phead;
	libo* cur1 = p1;
	libo* cur2 = p2;
	while (cur1 && cur2)
	{
		if (cur1->v > cur2->v)
		{
			cur->next = cur2;
			cur1 = cur1->next;
		}
		else
		{
			cur->next = cur1;
			cur1 = cur1->next;
		}
		cur = cur->next;
	}
	libo* tmp = cur1 ? cur1 : cur2;
	while (tmp)
	{
		cur->next = tmp;
		tmp = tmp->next;
		cur = cur->next;
	}
	return phead->next;
}

int main()
{
	libo* p1 = CreNd(0);
	libo* p2 = CreNd(0);
	libo* ph1 = CreNd(0);
	ph1 = p1;
	libo* ph2 = CreNd(0);
	ph2 = p2;
	for (int i = 0; i <= 2; i++)
	{
		libo* cur = CreNd(i);
		ph1->next = cur;
		ph1 = cur;
		
	}
	for (int i = 1; i <= 3; i++)
	{
		libo* cur = CreNd(i);
		ph2->next = cur;
		ph2 = cur;

	}

	libo* end = He_Bin(p1, p2);
	libo* tmpn = end;
	while(tmpn)
	{
		printf("%d ", tmpn->v);
		tmpn = tmpn->next;
	}
	/*while (end)
	{
		libo* ene = end->next;
		free(end);
		end = ene;
	}*/
	//free////
	return 0;
}